package com.health;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.et_password)
    EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btn_login)
    public void onClick() {
        doLogin();
    }

    private void doLogin() {
        if (isLoginSuccess()){
            Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            startActivity(new Intent(MainActivity.this,ChooseActivity.class));
        }
    }

    private boolean isLoginSuccess() {
        String name=etUsername.getText().toString().toLowerCase().trim();
        String pwd=etPassword.getText().toString().toLowerCase().trim();
        if (TextUtils.isEmpty(name)){
            etUsername.setError("用户名不能为空");
            return false;
        }
        if (TextUtils.isEmpty(pwd)){
            etPassword.setError("密码不能为空");
            return false;
        }
        if (pwd.length()<4){
            etPassword.setError("密码长度不足4位");
            return false;
        }
        if (name.equals("aaa")&&pwd.equals("111111")){
            MyApp.user=0;
            return true;
        }else if(name.equals("bbb")&&pwd.equals("111111")){
            MyApp.user=1;
            return true;
        }else{
            Toast.makeText(MainActivity.this,"账号密码错误",Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}
